#!/usr/bin/env bash

mkdir foo
cat <<EOF >mise.toml
[tools]
dummy = 'latest'
[hooks]
enter = 'echo HOOK-ENTER'
leave = 'echo HOOK-LEAVE'
cd = 'echo HOOK-CD'
preinstall = 'echo PREINSTALL'
postinstall = 'echo POSTINSTALL'
EOF

assert_contains "mise i 2>&1" "PREINSTALL"
assert_contains "mise i dummy@1 2>&1" "POSTINSTALL"
assert_contains "mise i 2>&1" "POSTINSTALL"
assert_contains "mise i dummy@1 2>&1" "POSTINSTALL"
eval "$(mise hook-env)"

cd ~/workdir || exit 1
assert_not_contains "mise hook-env 2>&1" "HOOK-CD"
assert_not_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_not_contains "mise hook-env 2>&1" "HOOK-LEAVE"

cd ~ || exit 1
assert_not_contains "mise hook-env 2>&1" "HOOK-CD"
assert_not_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_contains "mise hook-env 2>&1" "HOOK-LEAVE"
eval "$(mise hook-env)"

cd ~/workdir || exit 1
assert_contains "mise hook-env 2>&1" "HOOK-CD"
assert_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_not_contains "mise hook-env 2>&1" "HOOK-LEAVE"
eval "$(mise hook-env)"

cd ~/workdir/foo || exit 1
assert_contains "mise hook-env 2>&1" "HOOK-CD"
assert_not_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_not_contains "mise hook-env 2>&1" "HOOK-LEAVE"
eval "$(mise hook-env)"

cd ~ || exit 1
assert_not_contains "mise hook-env 2>&1" "HOOK-CD"
assert_not_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_contains "mise hook-env 2>&1" "HOOK-LEAVE"
eval "$(mise hook-env)"

cd ~/workdir/foo || exit 1
assert_contains "mise hook-env 2>&1" "HOOK-CD"
assert_contains "mise hook-env 2>&1" "HOOK-ENTER"
assert_not_contains "mise hook-env 2>&1" "HOOK-LEAVE"
eval "$(mise hook-env)"
